const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            category: [],
            member: [],
            person: [],
            city1: [],
            qwer: [],
            options: [],
            childDataForm1: [{}],
            is: true,
            fileList: [],
            showDialog: false,
            fileUrl: '',
        };
    },
    mounted() {

        // 获取工作队成员数据
        this.fetchMemberoptions();

        this.childDataForm = [];

        this.getVillage();
        $.ajax({
            headers: requestHeaders(),
            //url: `${mh}/generalBusiness/getDistrictGridTree`,

            url: `${mh}/clientOperation/getDistrictTree`,
            type: "GET",
            data: {
                DistrictCode: "232723"
            },
            success: (data) => {
                this.city1 = data[0].children;
            },
            complete: (XMLHttpRequest) => {
                requestPretreatment(XMLHttpRequest);
            },
            dataType: "json",
        });


        let ls_URL = window.location.href;
        this.action = getQueryObject(ls_URL, "action");
        this.nodeKey = getQueryObject(ls_URL, "nodeKey");

        if (this.action === 'Modify') {
            this.fetchData();
        }

        $.ajax({
            headers: requestHeaders(),
            url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
            method: "post",
            data: {
                SQLSerialNumber: 'f_work_teams_members',
                Condition: '',
                ExtensionCondition: '',
            },
            success: (data) => {
                console.log(data);

            }, complete: (XMLHttpRequest) => {
                requestPretreatment(XMLHttpRequest)
            },
        })

    },
    watch: {
        childDataForm(val) {
            if (this.action == 'Modify') {
                let arr = []
                val.forEach(item => {
                    item.name = item.file_nameOriginal + "（磁盘文件名:" + item.file_name + "）"

                    if (item.Row_Status == '0') {
                        arr.push(item)
                    }

                })
                this.fileList = arr;

            }
        },
        // 'dataForm.Village_Number'(val) {
        //     if (this.action == 'Modify' && this.is) {
        //         this.is = false;
        //         let str = this.dataForm.Village_Number
        //         console.log(this.dataForm);
        //         $.ajax({
        //             headers: requestHeaders(),
        //             //url: `${mh}/generalBusiness/getDistrictGridTree`,
        //             url: `${mh}/clientOperation/getDistrictTree`,
        //             type: "GET",
        //             data: {
        //                 DistrictCode: str.substring(0, 6)
        //             },
        //             success: (data) => {


        //                 this.qwer = [str.substring(0, 9), str];

        //                 // this.cityValue1 = [str.substring(0, 9), str];
        //                 // this.cityValue = [str.substring(0, 2) + '0000', str.substring(0, 4) + '00', str.substring(0, 6)]
        //             },
        //             complete: (XMLHttpRequest) => {
        //                 requestPretreatment(XMLHttpRequest);
        //             },
        //             dataType: "json",
        //         });

        //         $.ajax({
        //             headers: requestHeaders(),
        //             url: `${mh}/generalBusiness/generalQueryByTableName`,
        //             type: "POST",
        //             data: {
        //                 Data: 'personal_information',
        //                 Condition: "Row_Status='0' AND RR_Village_Number='" + str + "'"
        //             },
        //             success: (data) => {
        //                 this.member = data; // 直接使用 this
        //             },
        //             complete: (XMLHttpRequest) => {
        //                 requestPretreatment(XMLHttpRequest);
        //             },
        //             dataType: "json",
        //         });

        //     }
        // },
    },
    methods: {

        fetchMemberoptions() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 'f_work_teams_members',
                    Condition: '',
                    ExtensionCondition: '',
                },
                success: (data) => {
                    // 将获取的数据赋值给 memberOptions
                    this.options = data;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest)
                },
            });
        },

        fetchData() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'work_teams_works_logs',
                    Condition: `Work_Log_SerialNumber='${this.nodeKey}'`
                },
                success: (data) => {
                    if (data && data.length > 0) {
                        this.dataForm = data[0];
                    }
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        FileUploadsuccess(row, res, Attachment_Type) {

            this.childDataForm.push({
                uid: row.uid,
                file_name: res.fileName,
                file_nameOriginal: row.name,
                file_path: res.tempPath,
                file_type: res.contentType,
                Row_Status: '0'
            })

        },

        FileUploadremove(row) {
            if (!Object.keys(row).some(item => item.includes('_Original'))) {
                this.childDataForm = this.childDataForm.filter(i => i.uid != row.uid);
            } else {
                let obj = this.childDataForm.filter(i => i.attachment_id == row.attachment_id)[0];
                obj.Row_Status = '1'
            }
        },
        handlepreview(row){
            this.fileUrl = photo + row.file_name;
            this.showDialog = true;
        },

        // handleSubmit() {
        //     this.$refs.form.validate((valid) => {
        //         if (valid) {
        //             const url = this.action === 'Add' ? 
        //                 `${mh}/generalBusiness/generalSaveByRequestBody` : 
        //                 `${mh}/generalBusiness/generalModify`;

        //             const params = {
        //                 TableName: 'work_teams_works_logs',
        //                 Data: JSON.stringify(this.dataForm),
        //                 Condition: this.action === 'Modify' ? 
        //                     `Work_Log_SerialNumber='${this.nodeKey}'` : ''
        //             };

        //             $.ajax({
        //                 headers: requestHeaders("JSON"),
        //                 url: url,
        //                 type: "POST",
        //                 data: JSON.stringify({
        //                     Data:{
        //                         Add:{
        //                             work_teams_works_logs:[this.dataForm]
        //                         }
        //                     }
        //                 }),
        //             success: (res) => {
        //                 console.log(res);
        //                 ElMessage.success(this.action === 'Add' ? '添加成功' : '修改成功');
        //                 setTimeout(() => {
        //                     window.location.href = '../a_Working_conditions/index.html';
        //                 }, 1500);
        //             },
        //             error: () => {
        //                     ElMessage.error('操作失败');
        //                 },
        //                 dataType: "json",
        //             });
        //         }
        //     });
        // },
        // add.js
        // handleSubmit() {
        //     this.$refs.form.validate((valid) => {
        //         if (valid) {
        //             // 统一使用 generalSaveByRequestBody 接口
        //             const url = `${mh}/generalBusiness/generalSaveByRequestBody`;

        //             // 添加时间戳确保每次请求唯一
        //             const timestamp = new Date().getTime();

        //             // 构建请求数据
        //             const requestData = {
        //                 Data: {
        //                     [this.action === 'Add' ? 'Add' : 'Modify']: {
        //                         work_teams_works_logs: [{
        //                             ...this.dataForm,
        //                             // 修改操作需要包含主键
        //                             ...(this.action === 'Modify' && { Work_Log_SerialNumber: this.nodeKey })
        //                         }]
        //                     }
        //                 }
        //             };

        //             // 如果是修改操作，添加条件
        //             if (this.action === 'Modify') {
        //                 requestData.Condition = `Work_Log_SerialNumber='${this.nodeKey}'`;
        //             }

        //             $.ajax({
        //                 headers: requestHeaders("JSON"),
        //                 url: url,
        //                 type: "POST",
        //                 data: JSON.stringify(requestData),
        //                 success: (res) => {
        //                     console.log(res);
        //                     ElMessage.success(this.action === 'Add' ? '添加成功' : '修改成功');
        //                     setTimeout(() => {
        //                         // 强制刷新页面
        //                         window.location.href = `../a_Working_conditions/index.html`;
        //                     }, 1500);
        //                 },
        //                 error: (xhr, status, error) => {
        //                     console.error('操作失败:', error);
        //                     ElMessage.error(`操作失败: ${xhr.responseText || error}`);
        //                 },
        //                 dataType: "json",
        //             });
        //         }
        //     });
        // },
        goBack() {
            window.history.back();
        },


        changeVillage(val) {
            let _this = this;
            let str = val[val.length - 1];
            this.dataForm.Village_Number = str;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'personal_information',
                    Condition: "Row_Status='0' AND RR_Village_Number='" + str + "'"
                },
                success: (data) => {
                    this.member = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });

            _this.dataForm.Captain_Number = '';
            _this.dataForm.Mobile = '';
        },
        changeMember(val) {
            let _this = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'personal_information',
                    Condition: "Row_Status='0' AND ID_Card_Number='" + val + "'"
                },
                success: (data) => {
                    // this.person = data; // 直接使用 this
                    _this.dataForm.Mobile = data[0]["Contact_Phone"]
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });

        },
        getVillage() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'base_system_village',
                    Condition: "TownNumber LIKE '232723%'",
                },
                success: (data) => {
                    this.village = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },

    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-online-preview', ylOnlinePreview);
app.component('yl-upload', ylUpload);
app.component('yl-text-editor', ylTextEditor);
app.component('yl-select', ylSelect);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");